package com.controller;

import com.google.gson.Gson;
import com.entry.Book;
import com.pojo.BooksMess;
import com.service.BookService;
import com.service.impl.BookServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/book/*")
public class BookServlet extends HttpServlet {

    private BookService bookService=new BookServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0.处理乱码：
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        System.out.println("===---====+++++++++");
        //1.获得请求路径
        String  url=request.getRequestURI();
        String methodName=url.substring(url.lastIndexOf("/")+1);
        Class cla=BookServlet.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(),request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    //查询全部数据
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ArrayList<Book> books = bookService.selectAll();
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(books));

    }

    //按条件查询数据
    public void queryByBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接收第一个参数：
        String query = request.getParameter("query");
        String str = request.getParameter("str");
        //准备参数：
        Book book=new Book();
        if("1".equals(query)){
            book.setBookName(str);
        }else if("2".equals(query)){
            book.setBookAuthor(str);
        }else if("3".equals(query)){
            book.setBookPublish(str);
        }
        ArrayList<Book> books = bookService.selectQuery(book);
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(books));
    }

    public void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获得参数：
        String bname=request.getParameter("bname");
        String aname=request.getParameter("aname");
        String cname=request.getParameter("cname");
        String page=request.getParameter("page");
        String price=request.getParameter("price");
        //准备参数：
        Book book=new Book();
        book.setBookName(bname);
        book.setBookAuthor(aname);
        book.setBookPublish(cname);
        book.setBookPage(Integer.parseInt(page));
        book.setBookPrice(Double.parseDouble(price));

        boolean b = bookService.insertBook(book);
        response.getWriter().println(b);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bid =Integer.parseInt(request.getParameter("bid"));
        BooksMess booksMess=new BooksMess();
        booksMess.setMess(bookService.deleteBook(bid)?"删除成功":"删除失败");
        booksMess.setBooks(bookService.selectAll());
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(booksMess));

    }

    public void queryByid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         int bid=Integer.parseInt(request.getParameter("bid"));
         Book book=bookService.selectById(bid);
         Gson gson=new Gson();
         response.getWriter().println(gson.toJson(book));

    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bname=request.getParameter("bname");
        String aname=request.getParameter("aname");
        String cname=request.getParameter("cname");
        int page=Integer.parseInt(request.getParameter("page"));
        double price=Double.parseDouble(request.getParameter("price"));
        String date=request.getParameter("date");
        int bid=Integer.parseInt(request.getParameter("bid"));

        Book book=new Book(bid,bname,aname,cname,page,price,date);

        boolean b = bookService.updateBook(book);
        response.getWriter().println(b);
    }
}